package com.wx.mall.operate.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.AdDto;
import com.wx.mall.operate.entity.Ad;
import com.wx.mall.operate.service.IAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-11
 */
@RestController
@RequestMapping("/ad")
@Api(tags = "广告",description = "AdController")
@Slf4j
public class AdController {

    @Autowired
    private IAdService service;


    @ApiOperation("获取数据列表（带筛选和排序）")
    @GetMapping("/getList")
    public CommonResult<CommonPage<Ad>> getList(@ApiParam("获取dto数据") AdDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

        List<Ad> list=service.getList(dto);

        return CommonResult.success(CommonPage.restPage(list));


    };

@ApiOperation("根据id查询广告")
@GetMapping("/selectOne")
public CommonResult<Ad> selectOne(Integer id){

    Ad ad=service.getById(id);

    return CommonResult.success(ad);

}

    @ApiOperation("修改广告")
    @PostMapping("/updateList")
    public CommonResult updateList(Ad ad){

        boolean suceess=service.updateById(ad);


        return suceess? CommonResult.success(): CommonResult.failed();

    };

    @ApiOperation("添加广告")
    @PostMapping("/addList")
    public CommonResult addList(Ad ad){

boolean suceess=service.save(ad);


return suceess? CommonResult.success(): CommonResult.failed();

    };

      @ApiOperation("删除单条数据(物理删除)")
      @DeleteMapping("/deleteOne/{id}")
      public CommonResult deleteOne(@PathVariable("id") @NotNull(message = "不能为空") Integer id){

          boolean sucess=service.removeById(id);
          return  sucess? CommonResult.success(): CommonResult.failed();

      }


    @ApiOperation(("上传图片"))
    @PostMapping("/uploadImg")
    public CommonResult uploadImg(@RequestParam MultipartFile file, @RequestParam Map<String ,String> param){

        String name=param.get("name").trim();
        Map<String,String> map=service.uploadImg(file,name);
      return CommonResult.success(map);

    }




}

